package com.wjk.kylin.mall.common.web.filter;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * Created by brian on 2017/5/29.
 */
public class WrappedHttpServletResponse extends HttpServletResponseWrapper {

    private ByteArrayOutputStream buffer;

    private WrappedServletOutputStream wrappedServletOutputStream;

    private PrintWriter printWriter;

    /**
     * Constructs a response adaptor wrapping the given response.
     *
     * @param response
     * @throws IllegalArgumentException if the response is null
     */
    public WrappedHttpServletResponse(HttpServletResponse response) throws IOException {
        super(response);

        /**
         * 替换默认的输出端，作为response输出数据的存储空间（即真正存储数据的流）
         */
        buffer = new ByteArrayOutputStream();
        /**
         * response输出数据时是调用getOutputStream()和getWriter()方法获取输出流，再将数据输出到输出流对应的输出端的。
         * 此处指定getOutputStream()和getWriter()返回的输出流的输出端为buffer，即将数据保存到buffer中。
         */
        wrappedServletOutputStream = new WrappedServletOutputStream(buffer);
        printWriter = new PrintWriter(new OutputStreamWriter(buffer, StandardCharsets.UTF_8));

    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return wrappedServletOutputStream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        return printWriter;
    }

    @Override
    public void flushBuffer() throws IOException {
        if(wrappedServletOutputStream != null){
            wrappedServletOutputStream.flush();
        }

        if(printWriter != null){
            printWriter.flush();
        }
    }

    @Override
    public void reset() {
        buffer.reset();
    }

    /**
     * 获取应答的参数
     */
    public String getResponseParams() throws IOException {
        return new String(buffer.toByteArray(), StandardCharsets.UTF_8);
    }

    /**
     * flush数据流，并且写回到源response中去
     * @throws IOException
     */
    public void flushToDest() throws IOException{
        flushBuffer();
        buffer.writeTo(getResponse().getOutputStream());
    }

    /**
     * 内部类，对ServletOutputStream进行包装，指定输出流的输出端
     */
    private class WrappedServletOutputStream extends ServletOutputStream {

        private ByteArrayOutputStream bos = null;

        public WrappedServletOutputStream(ByteArrayOutputStream stream) throws IOException {
            this.bos = stream;
        }

        /**
         * 将指定字节写入输出流bos
         * @param b
         * @throws IOException
         */
        @Override
        public void write(int b) throws IOException {
            bos.write(b);
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setWriteListener(WriteListener writeListener) {

        }
    }
}
